package com.essearch.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

public class GenRepository {


    // 指定实体生成所在包的路径
    private String packageOutPath = "com.essearch.repository";

    public  GenRepository(String tablename){
        StringBuilder sb = new StringBuilder("package ").append(packageOutPath).append(";\r\n\r\n");
        sb.append("import com.essearch.entity.").append(tablename).append(";\r\n");
        sb.append("import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;\r\n" +
                "import org.springframework.stereotype.Component;\r\n\r\n" +
                "@Component\r\n");
        sb.append("public interface ").append(tablename).append("Repository extends ElasticsearchRepository<")
                .append(tablename).append(",String> {\r\n}");
        try {
            File directory = new File("");
            String path = this.getClass().getResource("").getPath();

//            System.out.println("directory.getAbsolutePath() = " + directory.getAbsolutePath());
//            System.out.println("path = " + path);

            String outputPath = directory.getAbsolutePath() + "/src/main/java/"
                    + this.packageOutPath.replace(".", "/") + "/"
                    + GenEntity.initcaps(tablename) + "Repository.java";

            System.out.println("outputPath = " + outputPath);
//            System.out.println("\nsb = " + sb.toString());

            FileWriter fw = new FileWriter(outputPath);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(sb.toString());
            pw.flush();
            pw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
//        String[] tables = {"item_01_ext", "item_01_list", "item_01_list2", "item_01_person", "item_02", "item_02_ext", "item_03", "item_03_20181222", "item_03_ext", "item_03_ext_20181222", "item_04", "item_04_ext", "item_05", "item_05_20181222", "item_05_ext", "item_06", "item_06_ext", "item_06_jfbz", "item_06_sbyq", "item_06_yjkfhd", "item_06_zonghe", "item_06_zycp", "item_06_zykt", "item_07", "item_07_ext", "item_08", "item_08_ext", "item_09", "item_09_cwzk", "item_09_fee", "item_09_ryjg", "item_09_yjfx", "item_10", "item_10_ext", "item_10_kjjlxm", "item_10_lw", "item_10_ptjd", "item_10_sqxzl", "item_10_zykjxm", "item_11", "item_11_ext", "item_11_hzjbqk", "item_12", "item_12_ext", "item_13", "item_14", "item_14_ext", "item_15", "item_15_ext", "item_16", "item_main", "item_person", "item_person_base", "item_person_edu", "item_person_ext", "item_person_work", "item_unit", "item_unit_ext"
//                ,"request_person", "request_person_ext", "request_unit", "request_unit_ext"};
//        Arrays.stream(tables).forEach(GenRepository::new);
        new GenRepository("Jhrw_unit");

//        Arrays.stream(tables).forEach(tablename -> {
//
////            System.out.println("    @Resource\n    private R" + tablename.substring(1) + "Repository r" + tablename.substring(1) + "Repository;");
//            System.out.print(" else if (list.get(0) instanceof " + GenEntity.initcaps(tablename) + "){\n" +
//                    "                elasticsearchTemplate.putMapping(" + GenEntity.initcaps(tablename) + ".class);\n\n" +
//                    "                ArrayList<" + GenEntity.initcaps(tablename) + "> arrayList = new ArrayList<>();\n" +
//                    "                list.forEach(o -> arrayList.add((" + GenEntity.initcaps(tablename) + ") o));\n" +
//                    "                result = " + tablename + "Repository.saveAll(arrayList);\n" +
//                    "            }");
//        });

    }
}
